package com.fanshuai.hash;

import java.util.Iterator;

public class LinkedHashTable implements HashTable {
    public static class Node {
        public String key;
        public int value;
        public Node next;

        public Node(String key, int value) {
            this.key = key;
            this.value = value;
            this.next = null;
        }
    }

    private Node[] table;
    private int size;

    private int defaultSize = 16;
    private double factor = 0.75;
    private int LIMIT = 4 * 1024 * 1024;

    public LinkedHashTable() {
        table = new Node[defaultSize];
        size = 0;
    }

    public void resize() {
        int newSize = calcSize();
        Node[] newTable = new Node[newSize];

        Node cur = null;
        for (int index = 0; index < table.length; index++) {
            if (table[index] == null) {
                continue;
            }

            cur = table[index];
            while (cur != null) {
                int newIndex = cur.key.hashCode() % newSize;

                Node newNode = new Node(cur.key, cur.value);
                Node last = newTable[newIndex];
                if (last == null) {
                    newTable[newIndex] = newNode;
                } else {
                    while (last.next != null) {
                        last = last.next;
                    }
                    last.next = newNode;
                }

                cur = cur.next;
            }
        }

        this.table = newTable;
    }

    private int calcSize() {
        if (2 * table.length < LIMIT) {
            return 2 * table.length;
        } else {
            return table.length + LIMIT;
        }
    }

    @Override
    public void insert(String key, int value) {
        if ((size + 1.0) / table.length >= factor) {
            resize();
        }

        int index = key.hashCode() % table.length;
        Node node = new Node(key, value);

        Node last = table[index];
        if (last == null) {
            table[index] = node;
        } else {
            while (last.next != null) {
                last = last.next;
            }
            last.next = node;
        }

        size++;
    }

    @Override
    public Integer find(String key) {
        int index = key.hashCode() % table.length;
        Node node = table[index];
        while (node != null) {
            if (node.key.equals(key)) {
                return node.value;
            }

            node = node.next;
        }

        return null;
    }

    @Override
    public void delete(String key) {
        int index = key.hashCode() % table.length;
        Node node = table[index];
        Node pre = table[index];

        while (node != null && !node.key.equals(key)) {
            pre = node;
            node = node.next;
        }

        if (node != null) {
            pre.next = node.next;
            node.next = null;
            size--;
        }
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean contains(String key) {
        return find(key) != null;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            int visitedSize = 0;
            int index = 0;
            Node cur = null;

            @Override
            public boolean hasNext() {
                return visitedSize < size;
            }

            @Override
            public Integer next() {
                if (cur == null) {
                    while (table[index] == null && index < table.length) {
                        index++;
                    }
                    cur = table[index];
                }

                visitedSize++;
                int value = cur.value;
                cur = cur.next;

                return value;
            }
        };
    }
}
